<?php
require('/fpdf/fpdf.php');

// Datos de la Conexión a la Base de Datos
$bd_servidor	= "localhost";
$bd_puerto	= "5432";
$bd_nombre	= "AutoGuayana";	
$bd_usuario	= "postgres";
$bd_clave	= "190193";

// Conexión a la Base de Datos
$bd_conexion = pg_connect("
        host=$bd_servidor 
        port=$bd_puerto 
        user=$bd_usuario 
        password=$bd_clave 
        dbname=$bd_nombre
");


$fd = $_POST['fecha_desde'];
$fh = $_POST['fecha_hasta'];
$empleado = $_POST['empleado'];


$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',11);
$pdf->Cell(0,10,'Autos Guayana C.A',0,1,'c');
$pdf->Cell(0,10,'RIF J-30248489-7',0,1,'c');
$pdf->Cell(0,10,'Direccion: Av. Las Americas, Puerto Ordaz, Edo. Bolivar',0,1,'c');
$pdf->Cell(0,10,'Telefonos: (0286) 9558677- 9552311',0,1,'c');



if($empleado != 0){

$pdf->Cell(0,10,'Reporte de ventas del empleado: '.$empleado,0,0,'C');    
    
$consulta= "SELECT emple.id_empleado, emple.nombre, emple.cedula, fac.id_factura, fac.monto, fac.porc_comision
FROM empleados as emple, facturas as fac, presupuestos as presu
WHERE fac.id_presupuesto = presu.id_presupuesto and emple.id_empleado = presu.id_empleado and emple.id_empleado = '$empleado' and fac.fecha_emision <= '$fd' and fac.fecha_emision >='$fh'; ";


$datos= pg_query($consulta);

  $nombre='y';      

           $pdf->Ln(); 
           $pdf->Ln(); 
               while ($registro= pg_fetch_array($datos)){
         if ($nombre != $registro['nombre']){ 
            $pdf->cell(10,5,'Ficha: ',0,0, 'C');
            $pdf->cell(20,5, $registro['id_empleado'],0,0, 'C');
            $pdf->cell(10,5, 'Cedula: ',0,0, 'C');
            $pdf->cell(30,5, $registro['cedula'],0,0, 'C');
            $pdf->cell(10,5, 'Nombre: ',0,0, 'C');   
            $pdf->cell(30,5, $registro['nombre'],0,0, 'C');
            $pdf->Ln();
            $pdf->Ln();
            $pdf->cell(20,5,'Num Fact.',1,0, 'C');
            $pdf->cell(30,5, 'Monto',1,0, 'C');
            $pdf->cell(30,5, 'Comision',1,0, 'C');
            
         }
            $nombre = $registro['nombre']; 
            $pdf->Ln();
            $pdf->cell(20,5, $registro['id_factura'],1,0, 'C');
            $pdf->cell(30,5, $registro['monto'],1,0, 'C');
            $pdf->cell(30,5, $registro['porc_comision'],1,0, 'C');
     
            
        }
        
       
    
    
}

else
{
     $pdf->Cell(0,10,'Reporte de ventas desde '. $fd . ' hasta ' . $fh ,0,0,'C'); 
     $pdf->Ln();
$consulta= "SELECT DISTINCT emple.id_empleado, emple.nombre, emple.cedula, auto.modelo, detalle.monto_final, fac.fecha_emision
FROM empleados as emple, facturas as fac, presupuestos as presu, detalle_presupuesto_vehiculo as detalle, automoviles as auto
WHERE  presu.id_presupuesto = fac.id_presupuesto  and emple.id_empleado = presu.id_empleado and detalle.id_presupuesto = presu.id_presupuesto and auto.id_automovil = detalle.id_automovil and fac.fecha_emision <= '$fd' and fac.fecha_emision >='$fh';";

$consulta2 = "SELECT monto_final FROM detalle_presupuesto_vehiculo;";
        
$datos= pg_query($consulta);
$datos2= pg_query($consulta2);
 // $datos2=  pg_query($consulta2);
$id=0; 
$subtotal=0;
$total=0;
$id2=0;
$contador=0;

for ($i=0; $i<5; $i++){
    
    $bv [$i] [0] = 0;
    $bv [$i] [1] = 0;
    $bv [$i] [2] = "";
    $bv [$i] [3] =  0;
    
}

    $aux [0] = $aux2 [0] = 0;
    $aux [1] = $aux2 [1] = 0;
    $aux [2] = $aux2 [2] = "";
    $aux [3] = $aux2 [3] = 0;

        while ($registro= pg_fetch_array($datos2)){
            $total+=$registro['monto_final'];
        }
        
        $datos= pg_query($consulta);
         while ($registro= pg_fetch_array($datos)){
            if ($id != $registro['id_empleado']){ 
            $pdf->Ln();
            $pdf->cell(10,5,'Ficha: ',0,0, 'C');
            $pdf->cell(20,5, $registro['id_empleado'],0,0, 'C');
            $pdf->cell(10,5, 'Cedula: ',0,0, 'C');
            $pdf->cell(30,5, $registro['nombre'],0,0, 'C');
            $pdf->cell(10,5, 'Nombre: ',0,0, 'C');   
            $pdf->cell(30,5, $registro['cedula'],0,0, 'C');
            $pdf->Ln();
            $pdf->Ln();
            $pdf->cell(30,5,'Automovil.',1,0, 'C');
            $pdf->cell(30,5, 'Monto',1,0, 'C');
            $pdf->cell(30,5, 'Fecha',1,0, 'C');
            $contador ++;
         }
            $id2= $id;
            $id = $registro['id_empleado']; 
            $pdf->Ln();
            $pdf->cell(30,5, $registro['modelo'],1,0, 'C');
            $pdf->cell(30,5, $registro['monto_final'],1,0, 'C');
            $pdf->cell(30,5, $registro['fecha_emision'],1,0, 'C');
            $subtotal+=$registro['monto_final'];
            $pdf->Ln();
            
   
            if ( $contador!=0 ){  
                $pdf->cell(30,5,'Total vendedor: ',0,0, 'C');
                $pdf->cell(20,5, $subtotal,0,0, 'C');
                $pdf->cell(30,5,'Porcentaje: ',0,0, 'C');
                $pdf->cell(30,5, (($subtotal/$total)*100).'%',0,0, 'C');
                
                for ($i=0; $i<5;$i++){
                    if($bv [$i][3] < $subtotal){
                       $aux [0] = $bv [$i] [0];
                       $aux [1] = $bv [$i] [1];
                       $aux [2] = $bv [$i] [2];
                       $aux [3] = $bv [$i] [3];
                       
                       $bv [$i] [0] = $registro['id_empleado'];
                       $bv [$i] [1] = $registro['cedula'];
                       $bv [$i] [2] = $registro['nombre'];
                       $bv [$i] [3] = $subtotal;
                       $b = $i+1;
                       while ($bv [$b] [0] == 0 and $aux [0]!=0){
                           
                       $aux2[0] = $bv [$b] [0];
                       $aux2[1] = $bv [$b] [1];
                       $aux2[2] = $bv [$b] [2];
                       $aux2[3] = $bv [$b] [3];
                       $bv [$b] [0] =  $aux [0];
                       $bv [$b] [1] =  $aux [1];
                       $bv [$b] [2] =  $aux [2];
                       $bv [$b] [3] =  $aux [3];
                       $aux [0] = $aux2[0];
                       $aux [3] = $aux2[1];
                       $aux [4] = $aux2[2];
                       $aux [5] = $aux2[3];
                       $b++;
                       }
                       $i=5;
                    }
                }
                $subtotal =0;
                $contador=0;
                $pdf->Ln();
                $pdf->Ln();
            }
      
            
      
            
        }
        
        
    
            $pdf->Cell(40,10,'Tabla de 5 mejores vendores vendedores',0,0,'c');
            $pdf->Ln();
            $pdf->cell(30,5,'Ficha: ',1,0, 'C');
            $pdf->cell(30,5, 'Cedula: ',1,0, 'C');
            $pdf->cell(30,5, 'Nombre: ',1,0, 'C');   
            $pdf->cell(30,5, 'Monto vendido ',1,0, 'C'); 
            for ($i=0; $i<5;$i++){
            $pdf->Ln();
            $pdf->cell(30,5, $bv[$i][0],1,0, 'C');
            $pdf->cell(30,5, $bv[$i][1],1,0, 'C');
            $pdf->cell(30,5, $bv[$i][2],1,0, 'C');
            $pdf->cell(30,5, $bv[$i][3],1,0, 'C');
             }
            
}

        $pdf->Output();